function totalNQueens(n) {
  let count = 0
  const dfs = (queens, xy_dif, xy_sum) => {
    const p = queens.length
    if (p === n) {
      count++
      return
    }
    for (let q = 0; q < n; q++) {
      if (!queens.includes(q) && !xy_dif.includes(p - q) && !xy_sum.includes(p + q)) {
        dfs(queens.concat(q), xy_dif.concat([p - q]), xy_sum.concat([p + q]))
      }
    }
  }
  dfs([], [], [])
  return count
}